Dir[File.expand_path('../models/*.rb', __FILE__)].each {|file| require file }
require 'oj'

get '/dashboardapi/section_progress/:id' do |id|
  content_type :json
  section = Section.find(id)
  result = {}
  result['script'] = {}
  result['script']['id'] = section.script.id
  result['script']['name'] = (section.script.name)
  result['script']['levels_count'] = section.script.levels.count
  result['script']['stages'] = section.script.stages.collect{|stage|{'length'=> stage.script_levels.count, 'title'=> I18n.t("data.script.name.#{section.script.name}.#{stage.name}")}}
  
  result['students'] = section.students.collect{|student|{'id' => student.id, 'levels' => []}}
  section.script.stages.each do |stage|
    stage.script_levels.each do |script_level|
      level_id = script_level.level_id
      result['students'].each do |student|
        ul_ex =  script_level.user_levels.where(user_id: student['id']).first
        ha = {'class' => (ul_ex.present? ? 'tried' : 'not_tried'), 'title' => script_level.position, 'url' => "http://studio.istemedu.com/s/#{section.script.name}/stage/#{stage.position}/puzzle/#{script_level.position}?section_id=#{section.id}&user_id=#{student['id']}"}
        if ul_ex.present?
          ha['class'] = 'attempted'
          ha['class'] = 'passed' if ul_ex.passing?
          ha['class'] = 'perfect' if ul_ex.best?
        end
        student['levels'] << ha
      end
    end
  end
  Oj.dump(result)
end

get '/dashboardapi/section_text_responses/:id' do |id|
  content_type :json
  results = []
  
  results.to_json
end

get '/dashboardapi/section_assessments/:id' do |id|
  content_type :json
  results = []
  
  results.to_json
end

get '/dashboardapi/user_menu' do
  dont_cache
  haml :user_header, format: :html5
end

get '/dashboardapi/user_hero' do;end